import {getStorage, removeStorage, setStorage} from 'jh-utils';
import history from "umi/router";
import {getLocale} from "umi-plugin-locale";
import {isEmpty, isFunction} from "lodash-es";
import {logout, userLogin} from "@/services/login";
import {fetchCurrentUserInfo} from "@/services/global";
import {getUserInfo} from "@/utils/storage";
import {STORAGE_KEY} from "@/globalConfig";

let locale = getStorage(STORAGE_KEY.LOCALE) || getLocale();
let userInfo = getUserInfo();
let accountInfo = getStorage(STORAGE_KEY.ACCOUNT_INFO);
let userDefaultInfo = getStorage(STORAGE_KEY.USER_DEFAULT_INFO);

const model = {
  namespace: 'global',
  state: {
    loginTimeout: false, // 标识是否登录会话过期了
    randomKey: '', // 随机数，用于强制重新渲染整个应用
    locale, // 当前语言类型
    userInfo,
    accountInfo,
    userDefaultInfo,
    collapsed: false,
  },
  effects: {
    * pswLogin({payload}, {call, put}) {
      const rsp = yield call(userLogin, payload);

      if (rsp && 200 === rsp.status) {

        yield put({
          type: 'afterLogin',
          payload: {rsp}
        });
      }

      return rsp;
    },
    // 登录成功/或者 iframe 关闭之后，会触发这个 action ，以更新 state 的公用数据
    * afterLogin({payload}, {put}) {
      const rsp = payload ? payload.rsp : {};
      let accountInfo;
      let userInfo;

      if (!isEmpty(rsp) && !isEmpty(rsp.data)) {
        accountInfo = rsp.data;
        userInfo = {
          id: accountInfo.userId,
          name: accountInfo.userName,
          phone: accountInfo.phone,
          code: accountInfo.userCode,
          email: accountInfo.email,
        };
        setStorage(STORAGE_KEY.USER_INFO, userInfo);
        setStorage(STORAGE_KEY.ACCOUNT_INFO, accountInfo);

      } else {
        userInfo = getStorage(STORAGE_KEY.USER_INFO);
        accountInfo = getStorage(STORAGE_KEY.ACCOUNT_INFO);
      }

      yield put({
        type: 'setStateByPayload',
        payload: {
          loginTimeout: false,
          userInfo,
          accountInfo
        },
      });
      // 获取当前用户信息
      // yield put({
      //   type: 'findSelfInfo',
      // });

      return rsp;

    },
    /**
     * 退出登录
     */* logout(action, {call, put}) {
      const response = yield call(logout);

      if (response && 200 === response.status) {
        yield put({
          type: 'afterLogout',
        });

        history.replace({
          pathname: '/login'
        });
      }
      return response;
    },
    /**
     * 注销之后，清空当前系统用户数据和缓存
     * @param action
     * @param call
     * @param put
     */
      * afterLogout(action, {call, put}) {
      removeStorage(STORAGE_KEY.USER_INFO);
      removeStorage(STORAGE_KEY.ACCOUNT_INFO);
      removeStorage(STORAGE_KEY.USER_DEFAULT_INFO);

      yield put({
        type: 'setStateByPayload',
        payload: {
          userInfo: {},
          accountInfo: {}
        },
      });
    },
    *findCurrentUserInfo({ payload }, { call, put }){
      const rsp = yield call(fetchCurrentUserInfo, { });
      if (rsp && 200 === rsp.status && rsp.data) {
        setStorage(STORAGE_KEY.USER_DEFAULT_INFO, rsp.data);
        let userDefaultInfo = rsp.data;
        yield put({
          type: 'setStateByPayload',
          payload: {
            userDefaultInfo
          },
        });
      }
      return rsp;
    }
    // * findSelfInfo({ payload }, { call, put }) {
    //   // 获取用户信息
    //   const info = getUserInfo() || {};
    //   if (info.id) {
    //     const rsp = yield call(findUserInfo, { id: info.id });
    //     if (rsp && 200 === rsp.status && rsp.data) {
    //       setStorage(STORAGE_KEY.USER_INFO, rsp.data);
    //       yield put({
    //         type: 'updateUserInfo',
    //         payload: rsp.data,
    //       });
    //     }
    //     return rsp;
    //   }
    // },
    // * findByCode({payload}, {put}) {
    //   return yield put({
    //     type: 'findByCodeFunc',
    //     payload,
    //   });
    // },
  },
  reducers: {
    setStateByPayload(state, {payload}) {
      return {...state, ...payload};
    }
  },
};
export default model;
